package com.GamersCom.model;

import java.security.InvalidParameterException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.GamersCom.db.*;
import com.GamersCom.util.GlobalCONST;
import com.GamersCom.util.Util;


/**
 * A user model handles the business logic about the users 
 * @author Frank
 *
 */
public class UserMgr
{
//	DAOFactory daoFactory = new DAOFactory();
	UserDAO userDao = DAOFactory.getUserDAO();
	
	/**
	 * Get all users from the users table
	 * @return List<User> list of users
	 */
	public List<User> getAllUsers()
	{
		String query = "select * from users order by startDate desc";
		return userDao.getUsers(query);
	}		
	
	/**
	 * Get the user with "approved" status
	 * @param username
	 * @param password
	 * @return User a user with "approved" status
	 */
	public User getApprovedUser(String username, String password)
	{
		String query = "select * from users where username= ? and password = ? and status = '"+GlobalCONST.APPROVED +"'";
		
		List paramList = new ArrayList();
		paramList.add(username);
		paramList.add(password);
		
		return userDao.getUser(query, paramList);
	}
	
	
	/**
	 * Get a user by username
	 * @param username
	 * @return User 
	 */
	public User getUserByUsername(String username)
	{
		String query = "select * from users where username= ?";
		
		List paramList = new ArrayList();
		paramList.add(username);
		
		return userDao.getUser(query, paramList);
	}
	
	/**
	 * Get a user by email
	 * @param email
	 * @return User
	 */
	public User getUserByEmail(String email) 
	{
		String query = "select * from users where email = ?";
		
		List paramList = new ArrayList();
		paramList.add(email);
		
		return userDao.getUser(query, paramList);
	}
	
	/**
	 * Get a user by token
	 * @param token
	 * @return User
	 */
	public User getUserByToken(String token)
	{
		String query = "select * from users where token = ?";
		
		List paramList = new ArrayList();
		paramList.add(token);
		
		return userDao.getUser(query, paramList);
	}
	
	
	/**
	 * Insert a user into User table
	 * @param user
	 * @throws ClassNotFoundException 
	 */
	public void insertUser(User user) throws SQLException, ClassNotFoundException
	{
		String query = "insert into users (username,password,firstname,lastname,email,status,accesslevel,token,startDate,securityQuestion,answer) values (?,?,?,?,?,?,?,?,?,?,?)";
		
		List paramList = new ArrayList();
		paramList.add(user.getUsername());
		paramList.add(user.getPassword());
		paramList.add(user.getFirstName());
		paramList.add(user.getLastName());
		paramList.add(user.getEmail());
		paramList.add(user.getStatus());
		paramList.add(user.getAccessLevel());
		paramList.add(user.getToken());
		paramList.add(new Date());
		paramList.add(user.getSecurityQuestion());
		paramList.add(user.getAnswer());
		
		//checks if the user exists or not		
		userDao.insertUser(query, paramList);
	}
	
	
	/**
	 * Update user profile 
	 * @param user
	 */
	public void updateUser(User user)
	{
		String query = "update users set firstname=?, lastname=?, password=?, securityQuestion=?, answer=? where id=?";
		
		List paramList = new ArrayList();
		paramList.add(user.getFirstName());
		paramList.add(user.getLastName());
		paramList.add(user.getPassword());
		paramList.add(user.getSecurityQuestion());
		paramList.add(user.getAnswer());
		paramList.add(user.getId());
	
		userDao.updateUser(query, paramList);
	}	
	
	
	
	
	/**
	 * Approve the user by changing the status from pending to approved
	 * @param userId
	 */
	public void approve(int userId)
	{
		String query = "update users set status=? where id = ?";
		
		List paramList = new ArrayList();
		paramList.add(GlobalCONST.APPROVED);
		paramList.add(userId);
		
		userDao.updateUser(query, paramList);
	}
	
	/**
	 * Reject the user and delete the table record
	 * @param userId
	 */
	public void reject(int userId)
	{
		String query = "delete from users where id = ?";
		
		List paramList = new ArrayList();
		paramList.add(userId);
		
		userDao.deleteUser(query, paramList);
	}
	
	/**
	 * Ban the user by changing the status to banned
	 * @param userId
	 */
	public void ban(int userId)
	{
		String query = "update users set status=? where id = ?";
		List paramList = new ArrayList();
		paramList.add(GlobalCONST.BANNED);
		paramList.add(userId);
		
		userDao.updateUser(query, paramList);		
	}

	
	/**
	 * Validate the security answer 
	 * @param user
	 * @param answer
	 * @return true if answer matches that in the database
	 */
	public boolean isValidAnswer(User user, String answer)
	{
		if ( user.getAnswer().equals(answer))
			return true;
		else 
			return false;
	}
	
}
